<template>
  <div>
    <!-- 添加或修改信息对话框 -->
    <el-dialog :title="title" v-bind="$attrs" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分组名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分组名称"/>
        </el-form-item>
        <el-form-item label="回显样式" prop="classStyle">
          <el-select v-model="form.classStyle">
            <el-option
              v-for="item in listClassOptions"
              :key="item.value"
              :label="item.label + '(' + item.value + ')'"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分组序号" prop="sortNum">
          <el-input v-model="form.sortNum" placeholder="请输入分组序号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStockGroup, addStockGroup, updateStockGroup } from '@/api/eye/stockGroup'

export default {
  name: 'AddOrUpdate',
  props: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 数据标签回显样式
      listClassOptions: [
        {
          value: 'default',
          label: '默认'
        },
        {
          value: 'primary',
          label: '主要'
        },
        {
          value: 'success',
          label: '成功'
        },
        {
          value: 'info',
          label: '信息'
        },
        {
          value: 'warning',
          label: '警告'
        },
        {
          value: 'danger',
          label: '危险'
        }
      ],
      // 弹出层标题
      title: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
  },
  methods: {
    // 表单重置
    reset() {
      console.log('重置')
      this.form = {
        id: null,
        stockCode: null,
        stockName: null,
        statisticsDays: null,
        riseDays: null,
        fallDays: null,
        continueRiseDays: null,
        continueRiseRate: null,
        continueFallDays: null,
        continueFallRate: null,
        riseStopDays: null,
        fallStopDays: null,
        continueRiseStopDays: null,
        continueFallStopDays: null,
        createTime: null,
        updateTime: null,
        kLineName: null
      }
      this.resetForm('form')
    },
    /** 添加股票提交按钮 */
    submitForm() {
      console.log('提交参数:' + JSON.stringify(this.form))
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStockGroup(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.reset()
              // 调用父组件的方法
              this.$emit('getList')
              this.$emit('update:visible', false)
            })
          } else {
            addStockGroup(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.reset()
              this.$emit('update:visible', false)
              // 调用父组件的方法
              this.$emit('getList')
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.$emit('update:visible', false)
      this.reset()
    },
    /** 新增按钮操作 */
    handleAdd() {
      console.log('这是子组件的添加弹窗')
      console.log(JSON.stringify(this.form))
      this.title = '添加股票基础信息'
      this.form.stockExchange = 'fund'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id
      getStockGroup(id).then(response => {
        this.form = response.data
        this.title = '修改股票指标统计信息'
      })
    }
  }
}
</script>

